package com.server.push.ws.base;

import com.server.dao.MobileAppDao;
import com.server.pojo.MobileApp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.websocket.Session;
import java.util.*;

@Configuration
@EnableScheduling    //开启定时任务
@Slf4j
public class WsSessions {
    public static Set<Session> all = new HashSet<>();
    /* 存储mobileApp的Session */
    public static Map<Integer,Session> mobileApps = new HashMap<>();

    @Autowired
    MobileAppDao mobileAppDao;

    public Set<MobileApp> getOnline(){
        Set<MobileApp> online = new HashSet<>();
        mobileApps.forEach((id, session) -> {
            if (session.isOpen()) {
                mobileAppDao.findById(id).ifPresent(online::add);
            }
        });
        return online;
    }

}
